﻿// Update 11 March 2012
//Added exception handling for the file
//Updated the filename to BrmConfig.xml from Brmconfig.xml as that was causing the restore to fail
//update 18 March 2012 - Formatted the xml and finally the brmconfig file is working along with other XML files.
//Ready for first commmit to codeplex.



using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;


namespace brmc
{
    class Program
    {
        static void Main(string[] args)
        {

            //=== Used for formatting the XML Output ====    
            var settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.NewLineChars = "\r\n"; 
            settings.OmitXmlDeclaration = true;
            settings.NewLineHandling = NewLineHandling.Replace;
            settings.IndentChars = "\t";

            try
            {

                //For reading the xml file
                XmlDocument readxml = new XmlDocument();
                XmlTextReader reader = new XmlTextReader(@"BRMDrivers.xml");
                readxml.Load(@"BRMDrivers.xml");

                //For creating the bmrconfig file
                XmlDocument xmlDoc = new XmlDocument();

                // xmlDoc.PreserveWhitespace = true;

                //Creating the nodes of the xml file
                XmlElement BrmConfig = xmlDoc.CreateElement("BrmConfig");
                XmlElement Plugins = xmlDoc.CreateElement("PLUGINS");
                XmlElement DriverMap = xmlDoc.CreateElement("DriverMap");

                BrmConfig.AppendChild(Plugins);
                BrmConfig.AppendChild(DriverMap);
                Plugins.AppendChild(xmlDoc.CreateTextNode(""));


                XmlNodeList elements = readxml.SelectNodes("//PRINTERDRIVERS/DRIVER");
                // Console.WriteLine("DriverName\t\t\tEnvironment");
                foreach (XmlElement element in elements)
                {
                    XmlElement DRV = xmlDoc.CreateElement("DRV");
                    DRV.SetAttribute("old", element.Attributes["DriverName"].Value);
                    DRV.SetAttribute("new", "Generic / Text Only");
                    DriverMap.AppendChild(DRV);
                }

                
                Console.WriteLine("PrintBRM Configurator - To speed up Print Server Migrations");
                Console.WriteLine("-----------------------------------------------------------");

                //Now Creating the PProcs.XML File
                //==================
                xmlDoc.AppendChild(BrmConfig);
                var w = XmlTextWriter.Create(@"BrmConfig.xml", settings);
                xmlDoc.Save(w);
                //xmlDoc.Save(@"BrmConfig.xml");
                
                
                Console.WriteLine("New BrmConfig.XML file created");

                //Now Creating the PProcs.XML File
                //====================
                XmlDocument xmlProc = new XmlDocument();
                XmlNode PPROCS = xmlProc.CreateElement("PPROCS");
                xmlProc.AppendChild(PPROCS);
                PPROCS.AppendChild(xmlProc.CreateTextNode(""));
                var pp = XmlTextWriter.Create(@"PPROCS.xml", settings);
                xmlProc.Save(pp);
                //xmlProc.Save(@"PPROCS.xml");
                Console.WriteLine("Clean PPROCS.XML file created");
                //=====================


                //Now Creating the BRMLMons.XML File
                //====================
                XmlDocument xmlMon = new XmlDocument();
                XmlElement LMONS = xmlMon.CreateElement("LMONS");
                LMONS.SetAttribute("Arch", "Windows x64");
                xmlMon.AppendChild(LMONS);
                var lm = XmlTextWriter.Create(@"BRMLMons.xml", settings);
                xmlMon.Save(lm);
                //xmlMon.Save(@"BRMLMons.xml");
                Console.WriteLine("Clean BRMLMons.XML file created");
                //=====================

                //Now Creating the BrmDrivers.XML File
                //====================
                XmlDocument xmlPDrv = new XmlDocument();
                XmlNode PRNDRV = xmlPDrv.CreateElement("PRINTERDRIVERS");
                xmlPDrv.AppendChild(PRNDRV);
                PRNDRV.AppendChild(xmlPDrv.CreateTextNode(""));
                var pd = XmlTextWriter.Create(@"BrmDrivers.xml", settings);
                xmlPDrv.Save(pd);
                //xmlPDrv.Save(@"BrmDrivers.xml");
                Console.WriteLine("Clean BrmDrivers.XML file created");
                //=====================

              
            }

            catch (Exception ex)
            {
                // Write error.
                Console.WriteLine(ex.Message);
            }

          

           Console.WriteLine("-----------------------------------------------------------");
           Console.Write("Press any key to exit");
           Console.ReadKey();


        }
    }
}
